﻿using UnityEngine;
using System.Collections;

public class PixelMask : MonoBehaviour 
{
    static int _pixelCount = 1280 * 720;
    public Texture2D _maskTexture;
	public Texture2D _diffuseTexture;
	    
    public Color[] _alphaColorArray;
    public Material _mat;

	public int _stageIdx = 0;
	
	void Start()
    {
        
	}

	public void Initialize( int stageIdx )
	{
		_stageIdx = stageIdx;

		WorldmapData worldmapData = WorldmapDataMgr.GetWorldmap (stageIdx);
		//worldmapData.diffuseTex

		SetDiffuseTexture (GameCommon.GetTexture (worldmapData.diffuseTex, LoadTextureType.Terrain));
		//SetMaskTexture (_maskTex);

		_alphaColorArray = WorldmapDataMgr.GetPixelAlpha(_stageIdx);                
		_alphaColorArray = _maskTexture.GetPixels();
		
		for (int i = 0; i < _alphaColorArray.Length; ++i)
		{
			if (_alphaColorArray[i].a > 0)
				_alphaColorArray[i].a = 1.0f;
		}
		
		WorldmapDataMgr.SetPixelAlpha(_stageIdx, _alphaColorArray);

		_mat.SetTexture("Base (RGB), Alpha (A)", _diffuseTexture);
		_mat.SetTexture("_AlphaTex", _maskTexture);
	}

	public void SetDiffuseTexture(Texture2D texture)
	{
		_diffuseTexture = texture;
	}

	public void SetMaskTexture (Texture2D texture)
	{
		_maskTexture = texture;
	}

    public void UpdatePixel()
    {
        _maskTexture.SetPixels(_alphaColorArray);
        _maskTexture.Apply();
    }
}